﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace PokeCrawl
{
    class Crawler
    {



        List<string> Pokemons = new List<string>();
        List<Pokemon> Pokemone = new List<Pokemon>();
        List<string> PkmnNames = new List<string>();
        int externPokemonCounter = 0;
        int externPokemonCounter2 = 0;
        int externPokemonCounter3 = 0;
        int externPokemonCounter4 = 0;
        int ID = 0;


        public List<string> DownloadWebPage(string Url)
        {
            List<string> _LinesOfWebpage = new List<string>();
            HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(Url);
            WebResponse Response = WebRequestObject.GetResponse();
            Stream WebStream = Response.GetResponseStream();
            StreamReader Reader = new StreamReader(WebStream);

            while (!Reader.EndOfStream)
            {
                _LinesOfWebpage.Add(Reader.ReadLine());

            }



            Reader.Close();
            WebStream.Close();
            Response.Close();

            return _LinesOfWebpage;
        }

        public void GetInformation()
        {
            List<string> _LinesOfWebpage = DownloadWebPage("http://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_index_number_(Generation_III)");
            char a = '"';

            Regex lookforInfo = new Regex("<td><a href=" + a + "/wiki/");



            for (int i = 0; i < _LinesOfWebpage.Count; i++)
            {
                if (lookforInfo.IsMatch(_LinesOfWebpage[i]))
                {
                    if (Pokemons.Count < 569)
                    {
                        string[] data = _LinesOfWebpage[i].Split('"');
                        if(!(Pokemons.Contains(data[1])))
                        {
                            Pokemons.Add(data[1]);
                        }
                       
                        
                    }
                    

                }
            }
            getPokemonNames();
           

        }

        public void getPokemonNames()
        {
            for (int i = 1; i < Pokemons.Count;i++)
            {
                int length = Pokemons[i].Split('/')[2].Split('(')[0].Length;
                if (!(PkmnNames.Contains(Pokemons[i].Split('/')[2].Split('(')[0].Remove(length - 1))))
                {
                    PkmnNames.Add(Pokemons[i].Split('/')[2].Split('(')[0].Remove(length - 1));
                }
                   
                   
              
            }
        }

        public List<string> GetPlainInformation(string url)
        {
            Regex xy = new Regex("<(.*?)>");




            List<string> listofPlain = new List<string>();
            List<string> PokemonNames = new List<string>();
            List<string> plainPokeText = new List<string>();
            // StreamWriter write = new StreamWriter("Pokeinfo.txt");
            for (int j = 1; j < Pokemons.Count; j++)
            {
                List<string> Pokemon = DownloadWebPage(url + Pokemons[j]);
               
               

                plainPokeText.Clear();
                for (int i = 0; i < Pokemon.Count; i++)
                {
                    if (xy.IsMatch(Pokemon[i]))
                    {

                        string plainText = (xy.Replace(Pokemon[i], ""));

                        if ((!(plainText == string.Empty)))
                        {
                            // write.WriteLine(plainText);
                            plainPokeText.Add(plainText);

                        }

                    }
                }


                Create(plainPokeText);

            }


            return listofPlain;


        }

        private void Create(List<string> plainPokeText)
        {
            string catchRate = "";
            string type = "";
            string height = "";
            string species = "";
            string weight = "";
            string description = "";
            int[] baseStats = new int[6];
            string gender = "";
            string baseXP = "";
            List<string> abilities = new List<string>();
            int[] ev = new int[6];
           
            for (int add = 0; add < plainPokeText.Count; add++)
            {
                switch (plainPokeText[add])
                {
                    case (" Catch rate"):
                        catchRate = plainPokeText[add + 1];
                        break;
                    case (" Types"):
                        type = plainPokeText[add + 1].Replace("&#160;","");
                        break;
                    case (" Species"):
                        species = plainPokeText[add + 1];
                        break;
                    case (" Height"):
                        height = plainPokeText[add + 2].Split('M')[0];
                        break;
                    case (" Weight"):
                        weight = plainPokeText[add + 2].Split('M')[0];
                        break;
                    case (" Gender ratio"):
                        gender = ((plainPokeText[add + 4].Replace("male","")).Replace("fe","")).Replace(',',';');
                        break;
                    case (" EV yield"):
                        int count = 1;
                        for (int i = 0; i <= 5; i++)
                        {
                            ev[i] = int.Parse(plainPokeText[add + count].Substring(plainPokeText[add + count].Length - 1).Trim());
                            count += 1;
                        }
                        break;
                    case (" Base stats"):
                        int counter = 6;
                        for (int i = 0; i <= 5; i++)
                        {
                            baseStats[i] = int.Parse(plainPokeText[add + counter]);
                            counter += 4;
                        }
                        break;
                    case (" {{{frlgdex}}}"):
                        description = plainPokeText[add + 2];
                        break;
                    case (" Abilities"):
                        int counterForAb = 1;
                        while ((plainPokeText[add+counterForAb] != " Exp. at Lv. 100"))
                        {
                            /*if (plainPokeText[add + counterForAb] == " ")
                            {
                                counterForAb++;
                            }*/
                            //else
                            {
                                abilities.Add(plainPokeText[add + counterForAb]);
                                counterForAb++;
                            }
                        }
                        
                        break;
                    case (" Ability"):
                        abilities.Add(plainPokeText[add + 1]);
                        break;






                }

                if (plainPokeText[add].Contains(" Base Exp.:"))
                {
                    baseXP = plainPokeText[add].Split(':')[1];
                }




            }



            int pokeindex = ID+1;
            if(ID==100)
            {
                string x = "";
            }
            CreatePokemon(PkmnNames[ID].Trim(), catchRate.Trim(), type.Trim(), height.Trim(), species.Trim(), weight.Trim(), description, baseStats, gender.Trim(), baseXP.Trim(), ev, pokeindex,abilities);
            ID++;
            
        }

        private void CreatePokemon(string name, string catchRate, string type, string height, string species, string weight, string description, int[] baseStats, string gender, string baseXP, int[] ev, int ID,List<string> abilities)
        {
            Pokemone.Add(new Pokemon(name, ID, type, species, height, weight, catchRate, gender, baseXP, ev, baseStats, description,GetMoves(),getTMHMAttacks(),getLearnMoves(),abilities));
       }

        public List<string[]> GetMoves()
        {
           
            
          
                Regex xy = new Regex("<(.*?)>");
                List<string[]> LevelUp = new List<string[]>();

                List<string> Information = DownloadWebPage("http://bulbapedia.bulbagarden.net/wiki/" +  PkmnNames[externPokemonCounter] + "_(Pok%C3%A9mon)/Generation_III_learnset#By_leveling_up");
                List<string> plainInformation = new List<string>();
               
                for(int j = 0; j < Information.Count;j++)
                {


                    if (xy.IsMatch(Information[j]))
                    {

                        string plainText = (xy.Replace(Information[j], ""));

                        if ((!(plainText == string.Empty)))
                        {
                            plainInformation.Add(plainText);               
                        }

                    }
                    
                }
                for (int z = 0; z < plainInformation.Count;z++ )
                {
                    switch (plainInformation[z])
                    {
                        case (" By leveling up"):
                            int counter1 = 13;
                            int counter2 = z;

                            while (!(plainInformation[z + counter1].Contains("obtained at level 1 in Generation III") || (plainInformation[z + counter1 + 1].Contains("obtained at level 1 in Generation III") ||
                                (plainInformation[z + counter1 + 2].Contains("obtained at level 1 in Generation III") || (plainInformation[z + counter1 + 3].Contains("obtained at level 1 in Generation III")
                                || (plainInformation[z + counter1 + 4].Contains("obtained at level 1 in Generation III") || (plainInformation[z + counter1 + 5].Contains("obtained at level 1 in Generation III")
                                || (plainInformation[z + counter1 + 6].Contains("obtained at level 1 in Generation III") || (plainInformation[z + counter1 + 6].Contains("obtained at level 1 in Generation III")
                                || (plainInformation[z + counter1 + 7].Contains("obtained at level 1 in Generation III")))))))))))
                            {
                                string[] attack = new string[9];
                                attack[0] = plainInformation[z + counter1];
                                attack[1] = plainInformation[z + counter1 + 1];
                                attack[2] = plainInformation[z + counter1 + 2];
                                attack[3] = plainInformation[z + counter1 + 3];
                                attack[4] = plainInformation[z + counter1 + 4];
                                attack[5] = plainInformation[z + counter1 + 5];
                                attack[6] = plainInformation[z + counter1 + 6];
                                attack[7] = plainInformation[z + counter1 + 7];
                                attack[8] = plainInformation[z + counter1 + 8];
                                LevelUp.Add(attack);
                                counter1 += 9;
                                counter2 += 9;
                            }

                            externPokemonCounter++;
                            return LevelUp;
                            
                            
                          
                    }
                   

                    
                }
                 
                
            

            return null;

        }

        public List<string[]> getTMHMAttacks()
        {
            StreamWriter xs = new StreamWriter("testpoke.txt");
            Regex xy = new Regex("<(.*?)>");
            List<string[]> TmHmAttacks = new List<string[]>();

            List<string> Information = DownloadWebPage("http://bulbapedia.bulbagarden.net/wiki/" + PkmnNames[externPokemonCounter2] + "_(Pok%C3%A9mon)/Generation_III_learnset#By_leveling_up");
            List<string> plainInformation = new List<string>();

            for (int j = 0; j < Information.Count; j++)
            {


                if (xy.IsMatch(Information[j]))
                {

                    string plainText = (xy.Replace(Information[j], ""));

                    if ((!(plainText == string.Empty)))
                    {
                        plainInformation.Add(plainText);
                    }

                }
               
                
            }
            for (int ss = 0; ss < plainInformation.Count; ss++)
            {
                xs.WriteLine(plainInformation[ss]);
            }
            xs.Close();
            for (int i = 0; i < plainInformation.Count(); i++)
            {
                switch (plainInformation[i])
                {
                    case ("Click on the generation numbers at the top to see level-up moves from other generations"):
                          int counter1 = 15;


                          while (!(plainInformation[i + counter1].Contains("indicates a move that gets") || (plainInformation[i + counter1 + 1].Contains("indicates a move that gets") ||
                                (plainInformation[i + counter1 + 2].Contains("indicates a move that gets") || (plainInformation[i + counter1 + 3].Contains("indicates a move that gets")
                                || (plainInformation[i + counter1 + 4].Contains("indicates a move that gets") || (plainInformation[i + counter1 + 5].Contains("indicates a move that gets")
                                || (plainInformation[i + counter1 + 6].Contains("indicates a move that gets") || (plainInformation[i + counter1 + 6].Contains("indicates a move that gets")
                                || (plainInformation[i + counter1 + 7].Contains("indicates a move that gets")))))))))))
                            {
                                string[] attack = new string[9];
                                attack[0] = plainInformation[i + counter1];
                                attack[1] = plainInformation[i + counter1 + 1];
                                attack[2] = plainInformation[i + counter1 + 2];
                                attack[3] = plainInformation[i + counter1 + 3];
                                attack[4] = plainInformation[i + counter1 + 4];
                                attack[5] = plainInformation[i + counter1 + 5];
                                attack[6] = plainInformation[i + counter1 + 6];
                                attack[7] = plainInformation[i + counter1 + 7];
                                attack[8] = plainInformation[i + counter1 + 8];
                                TmHmAttacks.Add(attack);
                                counter1 += 10;
                               
                            }

                          externPokemonCounter2++;
                            return TmHmAttacks;
                            
                            
                          
                  
                }
            }
            return null;
        }


        public List<string[]> getEggMoves()
        {

            Regex xy = new Regex("<(.*?)>");
            List<string[]> EggMoves = new List<string[]>();

            List<string> Information = DownloadWebPage("http://bulbapedia.bulbagarden.net/wiki/" + PkmnNames[externPokemonCounter3] + "_(Pok%C3%A9mon)/Generation_III_learnset#By_leveling_up");
            List<string> plainInformation = new List<string>();

            for (int j = 0; j < Information.Count; j++)
            {


                if (xy.IsMatch(Information[j]))
                {

                    string plainText = (xy.Replace(Information[j], ""));

                    if ((!(plainText == string.Empty)))
                    {
                        plainInformation.Add(plainText);
                    }

                }

            }
            for (int i = 0; i < plainInformation.Count(); i++)
            {
                switch (plainInformation[i])
                {
                    case ("Click on the generation numbers at the top to see TM moves from other generations"):
                        int counter1 = 15;


                        while (!(plainInformation[i + counter1].Contains("‡") || (plainInformation[i + counter1 + 1].Contains("‡") ||
                              (plainInformation[i + counter1 + 2].Contains("‡") || (plainInformation[i + counter1 + 3].Contains("‡")
                              || (plainInformation[i + counter1 + 4].Contains("‡") || (plainInformation[i + counter1 + 5].Contains("‡")
                              || (plainInformation[i + counter1 + 6].Contains("‡") || (plainInformation[i + counter1 + 6].Contains("‡")
                              || (plainInformation[i + counter1 + 7].Contains("‡")))))))))))
                        {
                            string[] attack = new string[8];
                            attack[0] = plainInformation[i + counter1];
                            attack[1] = plainInformation[i + counter1 + 1];
                            attack[2] = plainInformation[i + counter1 + 2];
                            attack[3] = plainInformation[i + counter1 + 3];
                            attack[4] = plainInformation[i + counter1 + 4];
                            attack[5] = plainInformation[i + counter1 + 5];
                            attack[6] = plainInformation[i + counter1 + 6];
                            attack[7] = plainInformation[i + counter1 + 7];
                           
                            EggMoves.Add(attack);
                            counter1 += 9;

                        }

                        externPokemonCounter3++;
                        return EggMoves;




                }
            }
            return null;
        }

        public List<string[]> getLearnMoves()
        {
            Regex xy = new Regex("<(.*?)>");
            List<string[]> TutorMoves = new List<string[]>();

            List<string> Information = DownloadWebPage("http://bulbapedia.bulbagarden.net/wiki/" + PkmnNames[externPokemonCounter4] + "_(Pok%C3%A9mon)/Generation_III_learnset#By_leveling_up");
            List<string> plainInformation = new List<string>();

            for (int j = 0; j < Information.Count; j++)
            {


                if (xy.IsMatch(Information[j]))
                {

                    string plainText = (xy.Replace(Information[j], ""));

                    if ((!(plainText == string.Empty)))
                    {
                        plainInformation.Add(plainText);
                    }

                }

            }
            for (int i = 0; i < plainInformation.Count(); i++)
            {
                switch (plainInformation[i])
                {
                    case ("Click on the generation numbers at the top to see Egg moves from other generations"):
                        int counter1 = 14;


                        while (!(plainInformation[i + counter1].Contains(" colored abbreviation in a white box indicates that") || (plainInformation[i + counter1 + 1].Contains(" colored abbreviation in a white box indicates that") ||
                              (plainInformation[i + counter1 + 2].Contains(" colored abbreviation in a white box indicates that") || (plainInformation[i + counter1 + 3].Contains(" colored abbreviation in a white box indicates that")
                              || (plainInformation[i + counter1 + 4].Contains(" colored abbreviation in a white box indicates that") || (plainInformation[i + counter1 + 5].Contains(" colored abbreviation in a white box indicates that")
                              || (plainInformation[i + counter1 + 6].Contains(" colored abbreviation in a white box indicates that") || (plainInformation[i + counter1 + 6].Contains(" colored abbreviation in a white box indicates that")
                              || (plainInformation[i + counter1 + 7].Contains(" colored abbreviation in a white box indicates that") || (plainInformation[i + counter1 + 8].Contains(" colored abbreviation in a white box indicates that")
                               || (plainInformation[i + counter1 + 9].Contains(" colored abbreviation in a white box indicates that") || (plainInformation[i + counter1 + 10].Contains(" colored abbreviation in a white box indicates that"))))))))))))))
                        {
                            string[] attack = new string[7];
                           
                          
                            
                            attack[0] = plainInformation[i + counter1 + 5];
                            attack[1] = plainInformation[i + counter1 + 6];
                            attack[2] = plainInformation[i + counter1 + 7];
                            attack[3] = plainInformation[i + counter1 + 8];
                            attack[4] = plainInformation[i + counter1 + 9];
                            attack[5] = plainInformation[i + counter1 + 10];
                            attack[6] = plainInformation[i + counter1 + 11];
                           

                            TutorMoves.Add(attack);
                            counter1 += 12;

                        }

                        externPokemonCounter4++;
                        return TutorMoves;




                }
            }
            return null;
        }




    }
    
}

